﻿package com.vfd.motion{

	import flash.display.DisplayObject;
	import flash.events.Event;

	public class Bouncer {
		private var _clip:DisplayObject;
		private var _leftBound:Number = 0;
		private var _topBound:Number = 0;
		private var _rightBound:Number = 0;
		private var _bottomBound:Number = 0;
		private var xVal:Number = 1
		private var yVal:Number = 1
		public function Bouncer(clip:DisplayObject) {
			this._clip = clip;
			this._clip.addEventListener(Event.ENTER_FRAME, checkBounds);
		}
		public function checkBounds(evt:Event) {
				this._clip.x += this.xVal;
				this._clip.y += this.yVal;
			if (this._clip.x + this._clip.width > this._rightBound) {
				trace("Clip has exceeded right bound")
				this._clip.x = this._rightBound - this._clip.width;
				this.xVal *= -1;
			}else if (this._clip.x - this._clip.width < this._leftBound) {
				trace("Clip has exceeded left bound")
				this._clip.x = this._leftBound + this._clip.width;
				this.xVal *= -1;
			}

			if (this._clip.y + this._clip.height > this._bottomBound) {
				trace("Clip has exceeded bottom bound")
				this._clip.y = this._bottomBound - this._clip.height;
				this.yVal *= -1;
			}else if (this._clip.y - this._clip.height < this._topBound) {
			trace("Clip has exceeded top bound")
				this._clip.y = this._topBound + this._clip.height;
				this.yVal *= -1;
			}
		trace("Clip x " + this._clip.x)
		}
		public function setBounds(top:Number, right:Number, bottom:Number, left:Number){
			this._topBound = top;
			this._rightBound = right;
			this._bottomBound = bottom;
			this._leftBound = left;
		}
		public function set speed(speed:Number) {
			this.xVal = speed
			this.yVal = speed
		}
		public function set xSpeed(speed:Number) {
			this.xVal = speed
		}
		public function set ySpeed(speed:Number) {
			this.yVal = speed
		}
	}
}